package com.tuju.jetpackfirstdemo.ui.link;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.tuju.jetpackfirstdemo.R;
import com.tuju.jetpackfirstdemo.ui.mpchart.utils.AnimationUtils;
import com.tuju.jetpackfirstdemo.ui.mpchart.utils.ChartUtils;
import com.tuju.jetpackfirstdemo.ui.mpchart.utils.ShowUtils;
import com.tuju.jetpackfirstdemo.utils.BaiZeUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class LinkActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tv_show;
    private Button btn_input1;
    private Button btn_input2;
    private TextView tv_tag_type;
    private ImageView iv_tag_type;

    private TextView tv_pop_test;
    private ImageView iv_pop_test;

    private List<String> mTagType = new ArrayList<>();
    private AutoCompleteTextView act;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_link);
        initView();
        mTagType.add("RLC-50-60/90");
        mTagType.add("RLC-50-90/120");
        mTagType.add("RLC-50-30/90");
        mTagType.add("RLC-50-30/60");
        mTagType.add("RLC-50-0/30");
    }

    private void initView() {
        tv_show = findViewById(R.id.tv_show);
        btn_input1 = findViewById(R.id.btn_input1);
        btn_input2 = findViewById(R.id.btn_input2);

        String[] province = getResources().getStringArray(R.array.province);
        ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_dropdown_item_1line, province);
        act = findViewById(R.id.AutoCompleteTextView01);
        act.setAdapter(adapter);
        btn_input1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String result = act.getText().toString();
                tv_show.setText(result);
            }
        });

        ArrayList<String> provinces = new ArrayList<>();
        for (int i = 0; i < province.length; i++) {
            provinces.add(province[i]);
        }
        AutoCompleteAdapter adapter2 = new AutoCompleteAdapter(LinkActivity.this,provinces);
        final AutoCompleteTextView act2 = findViewById(R.id.AutoCompleteTextView02);
        act2.setAdapter(adapter2);
        btn_input2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String result = act2.getText().toString();
                tv_show.setText(result);
            }
        });

        //popWindow标签类型
        tv_tag_type = findViewById(R.id.tv_tag_type);
        iv_tag_type = findViewById(R.id.iv_tag_type);
        tv_tag_type.setOnClickListener(this);
        iv_tag_type.setOnClickListener(this);

        tv_pop_test = findViewById(R.id.tv_pop_test);
        iv_pop_test = findViewById(R.id.iv_pop_test);
        tv_pop_test.setOnClickListener(this);
        iv_pop_test.setOnClickListener(this);
    }

    private boolean isFirst = true;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_tag_type:
            case R.id.iv_tag_type:
                String data = tv_tag_type.getText().toString();
                if (!ShowUtils.isPopupWindowShowing()) {
                    AnimationUtils.startModeSelectAnimation(iv_tag_type, true);
                    ShowUtils.showPopupWindow(this, tv_tag_type, tv_tag_type.getWidth(), ViewGroup.LayoutParams.WRAP_CONTENT, mTagType,
                            new AdapterView.OnItemClickListener() {
                                @Override
                                public void onItemClick(AdapterView<?> parent, View view,
                                                        int position, long id) {
                                    ShowUtils.updatePopupWindow(position);
                                    AnimationUtils.startModeSelectAnimation(iv_tag_type, false);
                                    ShowUtils.popupWindowDismiss();
                                    tv_tag_type.setText(mTagType.get(position));
                                }
                            });
                } else {
                    AnimationUtils.startModeSelectAnimation(iv_tag_type, false);
                    ShowUtils.popupWindowDismiss();
                }
                break;
            case R.id.tv_pop_test:
            case R.id.iv_pop_test:
                if (isFirst) {
                    mTagType.add("新增条目");
                }else{
                    mTagType.add("新增条目2");
                }
                isFirst = !isFirst;
                PopWindowsUtil.showPopupWindow(mTagType, tv_pop_test, new PopWindowsUtil.PopupSelectCallBack() {
                    @Override
                    public void selectItem(String str) {
                        tv_pop_test.setText(str);
                    }
                });
//                final PopupWindow popWindow = new PopupWindow(LinkActivity.this);
//                View contentView = LayoutInflater.from(LinkActivity.this).inflate(R.layout.dialog_list, null);
//                RecyclerView recyclerView = contentView.findViewById(R.id.rv_text);
//                FrameLayout frameLayout = contentView.findViewById(R.id.fl_pop_parent);
//                frameLayout.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        popWindow.dismiss();
//                    }
//                });
//                recyclerView.setLayoutManager(new LinearLayoutManager(LinkActivity.this));
//                TextAdapter textAdapter = new TextAdapter();
//                textAdapter.addAll(mTagType);
//                textAdapter.setCallBack(new TextAdapter.TextAdapterCallBack() {
//                    @Override
//                    public void onItemClickCallback(String content, int position) {
//                        tv_pop_test.setText(content);
//                        popWindow.dismiss();
//                    }
//                });
//                recyclerView.setAdapter(textAdapter);
//
//                popWindow.setContentView(contentView);
//                popWindow.setAnimationStyle(R.style.PopWindowAnimation);
//                popWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
//                popWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
//                popWindow.setBackgroundDrawable(null);//设置空背景 不设置背景图片点击外部不会消失
//                popWindow.setOutsideTouchable(true);//点击外部消失
//                //show
//                popWindow.showAsDropDown(tv_pop_test);
                break;
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                BaiZeUtil.hideKeyboard(ev,act,this);
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

}
